<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace app\common\model;

use think\facade\Db;

class UserLog extends Common
{

    const USER_LOGIN    = 'user login';     //登录
    const USER_LOGOUT   = 'user logout';    //退出
    const USER_REG      = 'user reg';    //注册
    const USER_EDIT     = 'user edit';    //用户编辑信息

    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');
        $where =[];

        $db_name = 'user_log';
        if(isset($post['select_year']) && !empty($post['select_year'])){
            if($post['select_year'] == '2021'){
                $db_name .= '_2021';
            }
        }
        if(isset($post['user_name']) && !empty($post['user_name'])){
            $where[] = ['username', 'like', '%'. trim($post['user_name']).'%'];
        }
        if(isset($post['state']) && !empty($post['state'])){
            $where[] = ['ul.state', '=', trim($post['state'])];
        }
        if(isset($post['desc']) && !empty($post['desc'])){
            $where[] = ['ul.params', 'like', '%'. trim($post['desc']).'%'];
        }
        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = strtotime($time_list[0] . " 00:00:00");
            $end_time   = strtotime($time_list[1] . " 23:59:59");
            $where[] = ['ul.ctime', 'between', "{$start_time},{$end_time}"];
        }

        $list = Db::name($db_name)->alias('ul')
            ->leftJoin('user u','u.id=ul.user_id')
            ->field('username,ul.ctime,ul.state,ul.params,ul.ip,ul.broswer')
            ->order('ul.ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

    /**
     * 添加日志
     * @param $user_id
     * @param $state
     * @param array $data
     */
    public function setLog( $user_id,$state,$data = [] )
    {
        $data = [
            'user_id'   => $user_id,
            'state'     => $state,
            'ctime'     => getUtcTime(),
            'params'    => json_encode($data),
            'ip'        => get_client_ip(0,true)
        ];
        $this->save($data);
    }

}